package ch.bfh.cmdh.thinkerforge.weatherstation.view;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JLabel;

import ch.bfh.cmdh.thinkerforge.weatherstation.controller.WeatherStationController;
import ch.bfh.cmdh.thinkerforge.weatherstation.model.ambientlight.AmbientLightSensor;
import ch.bfh.cmdh.thinkerforge.weatherstation.model.humidity.HumiditySensor;
import ch.bfh.cmdh.thinkerforge.weatherstation.model.temperature.TemperatureSensor;
import ch.bfh.cmdh.thinkerforge.weatherstation.view.subpanels.SimpleDataSheet;

public class WeatherStationMainFrame  extends JFrame implements Observer{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private SimpleDataSheet dataSheet;
    private WeatherStationController weatherStationController;
    
    public WeatherStationMainFrame(WeatherStationController weatherStationController){
        this.weatherStationController = weatherStationController;
        setTitle("TinkerForge");
        setSize(800, 150);
        setLayout(new BorderLayout());
        dataSheet = new SimpleDataSheet();
        JLabel titel = new JLabel("Super Hyper Mega Weather-Station");
        Font titleFont = new Font("Titlefont", Font.BOLD, 30);
        titel.setFont(titleFont);
        
        add(titel, BorderLayout.NORTH);
        add(dataSheet, BorderLayout.CENTER);
    
        addWindowListener(new WindowAdapter());
        setVisible(true);
    }
    
    private class WindowAdapter extends java.awt.event.WindowAdapter{
        
        @Override
        public void windowClosing(WindowEvent e) {
            weatherStationController.disconnectAllBricks();
            super.windowClosing(e);
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        HashMap<String, Integer> map = (HashMap<String, Integer>) arg;
        if (map.containsKey(AmbientLightSensor.AMBIENT_LIGHT_KEY)) {
            dataSheet.setAmbientValue(map.get(AmbientLightSensor.AMBIENT_LIGHT_KEY));
        }
        if (map.containsKey(TemperatureSensor.TEMPERATURE_KEY)) {
            dataSheet.setTempValue(map.get(TemperatureSensor.TEMPERATURE_KEY));
        }
        if (map.containsKey(HumiditySensor.HUMIDITY_KEY)) {
            dataSheet.setHumidityValue(map.get(HumiditySensor.HUMIDITY_KEY));
        }
        
    }
    
}
